package com.monster.servlet;

import com.alibaba.fastjson.JSONArray;
import com.monster.pojo.Playlist;
import com.monster.pojo.Tag;
import com.monster.service.ListServiceImpl;
import com.monster.service.PlaylistService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet("/CSXServlet")
public class CSXServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        String newPage = request.getParameter("page");
        PrintWriter out = null;
        List<Playlist> playlists = null;
        int page = Integer.parseInt(newPage);
        Tag tag = new Tag();
        PlaylistService playlistService = new ListServiceImpl();
        if(opr.equals("1")){
            playlists = playlistService.playlists(page,5);
        }else if(opr.equals("2")){
            tag.setTagType("流派");
        }else if(opr.equals("3")){
            tag.setTagType("语种");
        }else if(opr.equals("4")){
            tag.setTagType("主题");
        }else if(opr.equals("5")){
            tag.setTagType("心情");
        }else if(opr.equals("6")){
            tag.setTagType("场景");
        }
        if(!opr.equals("1")){
            playlists = playlistService.findPlaylistByTagType(tag,page);
        }

        try {
            out = response.getWriter();
            out.print(JSONArray.toJSON(playlists));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
}
